<?php 

// custom menu
class My_Category_Walker extends Walker_Category
{

	var $lev = -1;
	var $skip = 0;
	static $current_parent;

	function start_lvl( &$output, $depth = 0, $args = array() )
	{
		$this->lev = $depth+1;
		$output .= "<ul class='sub_categories level" .$this->lev. " '>" . PHP_EOL;
	}

	function end_lvl( &$output, $depth = 0, $args = array() )
	{
		$output .= "</ul>" . PHP_EOL;
		$this->lev = -1;
	}

	function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 )
	{
		extract($args);

		$cat_name = esc_attr( $category->name );
		$class_current = $current_class ? $current_class . ' ' : 'current ';

		if ( ! empty($current_category) )
		{
			$_current_category = get_term( $current_category, $category->taxonomy );
			if ( $category->term_id == $current_category ) $class = $class_current;
			elseif ( $category->term_id == $_current_category->parent ) $class = rtrim($class_current) . '-parent ';
		}
		else
		{
			$class = '';
		}
		if ( ! $category->parent )
		{
			if ( ! get_term_children( $category->term_id, $category->taxonomy ) )
			{
				$this->skip = 1;
				$this->lev = 0;
				$link = get_term_link(intval($category->term_id) , $category->taxonomy);
				if ( $category->term_id != $current_category ) $class_current= '';
				$output .= "<li class='level" . $this->lev . " " .$class_current. "'><a href='".$link."'>" . $category->name . "</a></li>\n";
			}
			else
			{
				if ($class == $class_current) self::$current_parent = $category->term_id;
				$output .= "<li class='" . $class . $level_class . "'>" . PHP_EOL;
				$output .= sprintf($parent_title_format, $cat_name) . PHP_EOL;
			}
		}
		else
		{
				
				
			if ( $this->lev == 0 && $category->parent)
			{
				$link = get_term_link(intval($category->parent) , $category->taxonomy);
				$stored_parent = intval(self::$current_parent);
				$now_parent = intval($category->parent);
				$all_class = ($stored_parent > 0 && ( $stored_parent === $now_parent) ) ? $class_current . ' all' : 'all';
				//$output .= "<li class='" . $all_class . "'><a href='" . $link . "'>" . __('All') . "</a></li>\n";
				self::$current_parent = null;
			}
				
			$link = '<a href="' . esc_url( get_term_link($category) ) . '" >' . $cat_name . '</a>';
			$output .= "<li";
			$class .= $category->taxonomy . '-item ' . $category->taxonomy . '-item-' . $category->term_id;
			$output .=  ' class="' . $class . '"';
			$output .= ">" . $link;
		}
	}

	function end_el( &$output, $page, $depth = 0, $args = array() ) {
		$this->lev++;
		if ( $this->skip == 1 ) {
			$this->skip = 0;
			return;
		}
		$output .= "</li>" . PHP_EOL;
	}

}

function custom_list_categories( $args = '' )
{
	$defaults = array(
			'taxonomy' => 'category',
			'show_option_none' => '',
			'echo' => 1,
			'depth' => 2,
			'exclude'	=> 1,
			'hide_empty' => 0,
			'wrap_class' => '',
			'level_class' => '',
			'parent_title_format' => '%s',
			'current_class' => 'current'
	);
	$r = wp_parse_args( $args, $defaults );
	if ( ! isset( $r['wrap_class'] ) ) $r['wrap_class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
	extract( $r );
	if ( ! taxonomy_exists($taxonomy) ) return false;
	$categories = get_categories( $r );

	$output = "<ul class='" . esc_attr( $wrap_class ) . "'>" . PHP_EOL;
	if ( empty( $categories ) )
	{
		if ( ! empty( $show_option_none ) ) $output .= "<li>" . $show_option_none . "</li>" . PHP_EOL;
	}
	else
	{
		if ( is_category() || is_tax() || is_tag() )
		{
			$current_term_object = get_queried_object();
			if ( $r['taxonomy'] == $current_term_object->taxonomy )
				$r['current_category'] = get_queried_object_id();
		}
		$depth = $r['depth'];
		$walker = new My_Category_Walker;
		$output .= $walker->walk($categories, $depth, $r);
	}
	$output .= "</ul>" . PHP_EOL;

	if ( $echo ) echo $output; else return $output;
}

// wp_title
/**
 * Title tag filter
 */
function yourthemeslug_title_filter( $title, $sep, $seplocation ) {

	// get the page number
	if( get_query_var( 'paged' ) )
		$page_num = get_query_var( 'paged' ); // on index
	elseif( get_query_var( 'page' ) )
	$page_num = get_query_var( 'page' ); // on single
	else $page_num = false;

	if(is_home() || is_front_page())
		$title = get_bloginfo('name');

	// strip title separator
	$title = trim( str_replace( $sep, '', $title ) );

	// determine order based on seplocation
	$parts = array($title, $page_num );
	if( $seplocation == 'left' )
		$parts = array_reverse( $parts );


	// strip blanks, implode, and return title tag
	$parts = array_filter( $parts );
	return implode( ' ' . $sep . ' ', $parts );

}
add_filter( 'wp_title', 'yourthemeslug_title_filter', 10, 3 );

// filter excerpt length
function custom_excerpt_length( $length ) {
	return 50;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

function db_init_meta_header()
{
	global $myoptions;
	if($myoptions['basic_robots_follow']=='display')
		$robots = 'index, follow';
	else 
		$robots = 'noindex, nofollow';
		
	$html.= '<meta name="robots" content="'.$robots.'" />';
	$html .= '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
	echo $html;
}

//task_cache('happy' , 'my_callback_function');
/**
 * @param int/string $cache_key
 * @param my function $callback
 * @param int $timers
 * @return mixed
 */
function db_callback_mydata($cache_key , $callback = false,$timers= false){
	global $myoptions;
	if($myoptions['basic_caches']=='display')
	{
		if($timers===false) $timers = 60*5;
		// xu ly cache
		$data = get_transient($cache_key);
		
		if ($data === false){
			//Now call the callback
			if($callback !== false){
				$data = call_user_func($callback);
				set_transient($cache_key, $data, $timers);
			}
		}	
	}	
	else
	{
		$data = call_user_func($callback);
	}
	//delete_transient($cache_key);
	return $data;
}
// process callback function basic setting
function db_basic_seting()
{
	return get_option("basic");
}




// process paginate in page/archive
function db_paginate()
{
	$prev = get_bloginfo('template_url')."/images/page_n.png";
	$next = get_bloginfo('template_url')."/images/page_r.png";

	global $wp_query, $wp_rewrite;
	$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
	$pagination = array(
			'base'         => @add_query_arg('paged','%#%'),
			'format'       => '',
			'total'        => $wp_query->max_num_pages,
			'current'      => $current,
			'show_all'     => false,
			'end_size'     => 1,
			'mid_size'     => 2,
			'prev_next'    => true,
			'prev_text'    => __('&laquo;'),
			'next_text'    => __('&raquo;'),
			'type'         => 'plain',
			'add_args'     => true,
		 'add_fragment' =>''  );

	if( $wp_rewrite->using_permalinks() )
		$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

	if( !empty($wp_query->query_vars['s']) )
		$pagination['add_args'] = array( 's' => str_replace(' ', '+',stripslashes(strip_tags(get_search_query())) ));

	return paginate_links( $pagination );
}

// process paginate in search page
function db_paginate_search()
{
	$prev = get_bloginfo('template_url')."/images/prev_03.jpg";
	$next = get_bloginfo('template_url')."/images/next_05.jpg";

	global $wp_query, $wp_rewrite;
	$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
	$pagination = array(
			'base'         => @add_query_arg('paged','%#%'),
			'format'       => '',
			'total'        => $wp_query->max_num_pages,
			'current'      => $current,
			'show_all'     => false,
			'end_size'     => 1,
			'mid_size'     => 2,
			'prev_next'    => true,
			'prev_text'    => __('&laquo;'),
			'next_text'    => __('&raquo;'),
			'type'         => 'plain',
			'add_args'     => true,
		 'add_fragment' =>''  );

	if( $wp_rewrite->using_permalinks() )
		$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );

	if( !empty($wp_query->query_vars['s']) )
		$pagination['add_args'] = array( 's' => str_replace(' ', '+',stripslashes(strip_tags(get_search_query())) ));

	return paginate_links( $pagination );
}
?>